package com.student.education.controller;

import com.student.education.common.CommonPage;
import com.student.education.common.CommonResult;
import com.student.education.entity.Clazz;
import com.student.education.entity.Major;
import com.student.education.entity.Role;
import com.student.education.service.MajorService;
import com.student.education.vo.MajorClassAddVo;
import com.student.education.vo.MajorClassSelectVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 专业表(Major)表控制层
 *
 * @author makejava
 * @since 2021-11-27 16:03:29
 */
@RestController
@RequestMapping("major")
public class MajorController {
    /**
     * 服务对象
     */
    @Resource
    private MajorService majorService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public Major selectOne(Integer id) {
        return this.majorService.queryById(id);
    }

    /**
     *  修改
     */
    @PostMapping("/update")
    public CommonResult<String> update(@RequestBody Major vo) {
        int i = majorService.update(vo);
        if (Objects.equals(i, 1)) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.error("修改失败");
    }

    /**
     *  新增
     */
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody Major vo){
        int i = majorService.insert(vo);

        if (Objects.equals(i, 1)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.error("添加失败");

    }

    /**
     *  删除
     */
    @GetMapping("delete")
    public CommonResult<String> delete(@RequestParam Integer id) {
        if (majorService.deleteById(id)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.error("删除失败");
    }

    /**
     *  查询没有专业的班级
     */
    @GetMapping("selectNoMajorClass")
    public CommonResult<List<Clazz>> selectNoMajorClass() {
        return majorService.selectNoMajorClass();
    }

    /**
     *  新增班级
     */
    @PostMapping("/addMajorClass")
    public CommonResult<String> addMajorClass(@RequestBody MajorClassAddVo vo){
        int i = majorService.addMajorClass(vo);

        if (i>0) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.error("添加失败");

    }
    /**
     *  新增班级
     */
    @GetMapping("/deleteMajorClass")
    public CommonResult<String> deleteMajorClass(Integer classId){
        int i = majorService.deleteMajorClass(classId);

        if (i>0) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.error("删除失败");

    }
    /**
     *  新增班级
     */
    @GetMapping("/listSelect")
    public CommonResult<List<MajorClassSelectVo>> listSelect(){
        return CommonResult.success(majorService.listSelect());
    }


}